/*
 * config.h
 *
 * Copyright 2011 Pieter Agten
 *
 * This file is part of Yarf.
 *
 * Yarf is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Yarf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Yarf.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   config.h
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   3 sep 2011
 * @brief  Contains all the user-configurable options of the firmware.
 */

#ifndef CONFIG_H
#define CONFIG_H

#include <math.h>

/* Basic firmware settings */
/**
 * Defines the electronics board for which the firmware will be compiled. The
 * list of available boards and the mapping from numbers to boards can be found
 * in \a boards.h.
 *
 * Valid values: 1 to the number of boards defined in \a boards.h
 */
#define BOARD 1

/**
 * The speed at which to communicate with the host computer. Setting this too
 * low will cause pauses when printing at high speeds, because movements will 
 * be executed faster than they are coming in. Setting this too high will cause
 * communication errors because the MCU can't keep up.
 *
 * Common serial baud rates include:
 *  - 2400
 *  - 4800
 *  - 9600
 *  - 19200 (recommended)
 *  - 38400
 *  - 57600
 *  - 115200
 */
#define HOST_BAUD_RATE            19200

/**
 * Determines whether the extruder coordinates received from the host (as part
 * of g-code commands) are interpreted as the length of filament to push into
 * the extruder, or the length of filament that needs to come out of the 
 * extruder.
 *
 * If this is set to 1, the coordinates are interpreted as the length of
 * filament to push IN to the extruder.
 * If this is set to 0, the coordinates are interpreted as the length of
 * filament that needs to come OUT of the extruder.
 * 
 * Valid values: 0 or 1
 */
#define E_COORDINATES_ARE_INPUT   1



/* Basic machine parameters */
/**
 * Determines whether or not to emulate the endstops at the end of each axis
 * (the max endstops) in software. If this parameter is set to 1, the max
 * endstops will be emulated in software. Otherwise, the actual hardware max
 * enstops will be used. If the selected board does not support max endstops,
 * this parameter has no effect (the max endstops will always be emulated in
 * software).
 *
 * Note: setting this parameter to 0 when there are no max endstops connected
 * can cause the carriage or print bed to crash into the end of an axis, which
 * will physically DAMAGE the machine!
 */ 
#define SOFTWARE_MAX_ENDSTOPS 1

/**
 * The maximum distance (in mm) the carriage can move from its origin on the
 * x axis.
 *
 * Note: setting this parameter too high can cause the carriage to crash into
 * the end of the x axis, which will physically DAMAGE the machine!
 *
 * Valid values: positive integers
 */
#define X_LENGTH 160

/**
 * The maximum distance (in mm) the print bed can move from its origin on the
 * y axis.
 *
 * Note: setting this parameter too high can cause the print bed to crash into
 * the end of the y axis, which will physically DAMAGE the machine!
 *
 * Valid values: positive integers
 */
#define Y_LENGTH 180

/**
 * The maximum distance (in mm) the z axis bed can move up from its origin.
 *
 * Note: setting this parameter too high can cause the extruder to crash into
 * the top of the machine, which will physically DAMAGE it!
 *
 * Valid values: positive integers
 */
#define Z_LENGTH 80



/**
 * The number of steps it takes the x axis' stepper motor to move the carriage
 * 1 mm.
 *
 * Valid values: all positive long integers
 */
#define X_STEPS_PER_MM  80L

/**
 * The number of steps it takes the y axis' stepper motor to move the carriage
 * 1 mm.
 *
 * Valid values: all positive long integers
 */
#define Y_STEPS_PER_MM  80L

/**
 * The number of steps it takes the z axis' stepper motors to move the z axis
 * 1 mm.
 *
 * Valid values: all positive long integers
 */
#define Z_STEPS_PER_MM  2560L


/**
 * Whether the direction of the x axis' stepper motor is inverted. Change this
 * value if the x axis' stepper motor is rotating in the wrong direction.
 *
 * Note: be very careful about changing this parameter, as it can cause the
 * carriage to crash into the end of the x axis during homing, which will
 * physically DAMAGE the machine!
 *
 * Valid values: 0 or 1
 */
#define X_DIR_INVERTED   0

/**
 * Whether the direction of the y axis' stepper motor is inverted. Change this
 * value if the y axis' stepper motor is rotating in the wrong direction.
 *
 * Note: be very careful about changing this parameter, as it can cause the
 * print bed to crash into the end of the y axis during homing, which will
 * physically DAMAGE the machine!
 *
 * Valid values: 0 or 1
 */
#define Y_DIR_INVERTED   1

/**
 * Whether the direction of the z axis' stepper motor is inverted. Change this
 * value if the z axis' stepper motor is rotating in the wrong direction.
 *
 * Note: be very careful about changing this parameter, as it can cause the
 * extruder to crash into top of the machine during homing, which will 
 * physically DAMAGE the machine!
 *
 * Valid values: 0 or 1
 */
#define Z_DIR_INVERTED   0

/**
 * Whether the direction of the extruder's stepper motor is inverted. Change 
 * this value if the extruder's stepper motor is rotating in the wrong
 * direction.
 *
 * Note: be careful about changing this parameter, as it can cause damage to
 * some types of extruders if set incorrectly.
 *
 * Valid values: 0 or 1
 */
#define E_DIR_INVERTED   1


/**
 * The absolute maximum speed (in mm/min) on the x axis.
 *
 * Valid values: positive integers
 */
#define X_MAX_SPEED                6000

/**
 * The absolute maximum speed (in mm/min) on the y axis.
 *
 * Valid values: positive integers
 */
#define Y_MAX_SPEED                6000

/**
 * The absolute maximum speed (in mm/min) on the z axis.
 *
 * Valid values: positive integers
 */
#define Z_MAX_SPEED                 170


/**
 * The maximum continuous speed (in mm/min) of filament going into the extruder.
 * This rate can be exceeded when the extruder performs advance steps due to the
 * the advance pressure management system (if enabled).
 *
 * Valid values: positive integers
 */
#define E_MAX_SPEED_IN              100

/**
 * The absolute maximum speed (in mm/min) of filament going into the extruder.
 * This rate will never be exceeded, even when the extruder is performing
 * advance steps due to the advance pressure management system.
 *
 * Valid values: positive integers greater than E_MAX_SPEED_IN
 */
#define E_MAX_ADVANCE_SPEED_IN      500




/* Filament and extruder parameters */
/**
 * The diameter of the filament going into the extruder in mm.
 *
 * Valid values: all positive floating point numbers
 */
#define FD 2.92

/**
 * The diameter of the filament coming out of the extruder in mm.
 *
 * Valid values: all positive floating point numbers
 */
#define ED 0.5

/**
 * The diameter of the extruder's pinch wheel in mm. That is, the outer
 * diameter of the teeth that push the filament through the extruder.
 *
 * Valid values: all positive floating point numbers
 */ 
#define PWD 6.6

/**
 * The extruder's gear ratio (dimensionless). That is, the number of rotations
 * the extruder stepper motor must make, in order for the pinch wheel to make
 * one rotation.
 *
 * Valid values: all positive floating point numbers
 */
#define GEAR_RATIO (39.0/11.0)

/**
 * The number of steps the extruder stepper motor takes in order to make one
 * full rotation. This can usually be calculated as:
 *   (360/degrees_per_step) * micro_stepping_rate
 *  where \a degrees_per_step is the number of degrees the stepper motor rotates
 *  for a single step and \a micro_stepping_rate is the stepper motor 
 *  controller's micro-stepping rate.
 * 
 * Valid values: all positive floating point numbers
 */
#define E_STEPS_PER_REV (360.0*8.0/1.8)





/* PID Temperature management settings */
/**
 * The number of times per second a temperature measurement and a heater power 
 * update for the nozzle and the print bed will be performed.
 *
 * Valid values: positive floating point numbers (but don't go over 2.0)
 */
#define HEATER_MANAGEMENT_FREQUENCY_HZ  1.0


/* Nozzle temperature settings */
/**
 * The MCU pin the nozzle heater is connected to. It is best to use a macro
 * defined in \a boards.h for the selected board.
 */
#define NOZZLE_HEATER_PIN              HEAT1_PIN

/**
 * The PWM register associated with the pin selected for the nozzle heater.
 *
 * Valid values: OCR0A or OCR0B
 */
#define NOZZLE_HEATER_PWM_REG          OCR0A

/**
 * The absolute maximum temperature the nozzle heater can be set to, in degrees
 * Celsius.
 *
 * Valid values: floating point numbers
 */
#define NOZZLE_HEATER_MAX_TEMP         250.0

/**
 * The maximum relative amount of power that can be applied to the nozzle
 * heater.
 *
 * Valid values: integers from 0 to 255
 */
#define NOZZLE_HEATER_MAX_DUTY_CYCLE   180

/**
 * The MCU pin the nozzle thermistor is connected to. It is best to use a macro
 * defined in \a boards.h for the selected board. The pin must be specified
 * by its ADC number (e.g. for ADC2 you should enter 2).
 */
#define NOZZLE_THERMISTOR_PIN          TEMP2_PIN

/**
 * The name of the thermistor table variable to use for converting the nozzle
 * thermistor's ADC values to degrees Celcius.
 * 
 * @see temperature/thermistor_table.h
 */
#define NOZZLE_THERMISTOR_TABLE        thermistor_table

/**
 * The number of degrees Celsius above or below the nozzle's target
 * temperature, at which the PID algorithm kicks in. Outside of this
 * temperature range, the heater is turned off completely if the temperature is
 * too high and the heater is turned on to its maximum duty cycle if the 
 * temperature is too low.
 *
 * Valid values: positive integers from 1 to 255
 */
#define NOZZLE_PID_ENVELOPE            10

/**
 * The gain of the proportional component of the nozzle heater's PID algorithm
 * instance.
 *
 * @see temperature/pid.h
 * 
 * Valid values: positive integers from 0 to 255
 */
#define NOZZLE_PID_P_GAIN              20

/**
 * The gain of the integral component of the nozzle heater's PID algorithm
 * instance. It is recommended to provide a constant divided by \a
 * HEATER_MANAGEMENT_FREQUENCY_HZ, as doubling the measurement frequency
 * would double the relative influence of the integral component otherwise.
 *
 * @see temperature/pid.h
 * 
 * Valid values: positive integers from 0 to 255
 */
#define NOZZLE_PID_I_GAIN              (1/HEATER_MANAGEMENT_FREQUENCY_HZ)

/**
 * The gain of the derivative component of the nozzle heater's PID algorithm
 * instance. It is recommended to provide a constant multiplied by \a
 * HEATER_MANAGEMENT_FREQUENCY_HZ, as doubling the measurement frequency
 * would half the relative influence of the integral component otherwise.
 *
 * @see temperature/pid.h
 * 
 * Valid values: positive integers from 0 to 255
 */
#define NOZZLE_PID_D_GAIN              (0*HEATER_MANAGEMENT_FREQUENCY_HZ)


/* Print bed temperature settings */
/**
 * The MCU pin the print bed heater is connected to. It is best to use a macro
 * defined in \a boards.h for the selected board.
 */
#define PRINTBED_HEATER_PIN            HEAT2_PIN

/**
 * The PWM register associated with the pin selected for the print bed heater.
 *
 * Valid values: OCR0A or OCR0B
 */
#define PRINTBED_HEATER_PWM_REG        OCR0B

/**
 * The absolute maximum temperature the print bed can be set to, in degrees
 * Celsius.
 *
 * Valid values: floating point numbers
 */
#define PRINTBED_HEATER_MAX_TEMP       130.0

/**
 * The maximum relative amount of power that can be applied to the print bed
 * heater.
 *
 * Valid values: integers from 0 to 255
 */
#define PRINTBED_HEATER_MAX_DUTY_CYCLE 255

/**
 * The MCU pin the print bed's thermistor is connected to. It is best to use a
 * macro defined in \a boards.h for the selected board. The pin must be
 * specified by its ADC number (e.g. for ADC2 you should enter 2).
 */
#define PRINTBED_THERMISTOR_PIN        TEMP1_PIN

/**
 * The name of the thermistor table variable to use for converting the print bed
 * thermistor's ADC values to degrees Celcius.
 * 
 * @see temperature/thermistor_table.h
 */
#define PRINTBED_THERMISTOR_TABLE      thermistor_table

/**
 * The number of degrees Celsius above or below the print bed's target
 * temperature, at which the PID algorithm kicks in. Outside of this
 * temperature range, the heater is turned off completely if the temperature is
 * too high and the heater is turned on to its maximum duty cycle if the 
 * temperature is too low.
 *
 * Valid values: positive integers from 1 to 255
 */
#define PRINTBED_PID_ENVELOPE          3

/**
 * The gain of the proportional component of the print bed heater's PID
 * algorithm instance.
 *
 * @see temperature/pid.h
 * 
 * Valid values: positive integers from 0 to 255
 */
#define PRINTBED_PID_P_GAIN            20

/**
 * The gain of the integral component of the print bed heater's PID algorithm
 * instance. It is recommended to provide a constant divided by \a
 * HEATER_MANAGEMENT_FREQUENCY_HZ, as doubling the measurement frequency
 * would double the relative influence of the integral component otherwise.
 *
 * @see temperature/pid.h
 * 
 * Valid values: positive integers from 0 to 255
 */
#define PRINTBED_PID_I_GAIN            (2/HEATER_MANAGEMENT_FREQUENCY_HZ)

/**
 * The gain of the derivative component of the print bed heater's PID algorithm
 * instance. It is recommended to provide a constant multiplied by \a
 * HEATER_MANAGEMENT_FREQUENCY_HZ, as doubling the measurement frequency
 * would half the relative influence of the integral component otherwise.
 *
 * @see temperature/pid.h
 * 
 * Valid values: positive integers from 0 to 255
 */
#define PRINTBED_PID_D_GAIN            (1*HEATER_MANAGEMENT_FREQUENCY_HZ)



/* Movements and acceleration settings */
/**
 * The default initial feed rate (in mm/min) for each movement. Each movement
 * will start at this speed and will accelerate up to the speed given in its
 * g-code command at a rate of \a ACCELERATION, unless the lookahead component
 * determines the movement can start at a higher speed without causing too much
 * jerkyness.
 *
 * Valid values: positive integers
 */
#define START_FEED_RATE 1000


/**
 * The acceleration and deceleration rate (in mm/min²) for all movements.
 *
 * Valid values: all positive floating point numbers (but too low numbers might 
 * cause overflow in the block handler component)
 */
#define ACCELERATION   2000000.0


/* Lookahead component settings */
/**
 * Whether to use the lookahead component. The lookahead component tries to
 * keep the speed at which movements are made as high as possible. That is, it
 * tries to raise the entry and exit speeds of blocks, while at the same time
 * preventing jerky movement.
 *
 * @see PLANNER_QUEUE_SIZE
 *
 * Valid values: 0 or 1
 */
#define PLANNER_LOOKAHEAD         1

/**
 * The maximum instantaneous speed difference (in mm/min) between two adjacent
 * perpendicular moves. The lookahead component uses this to determine the
 * maximum amount of jerkyness to allow between two moves.
 *
 * Valid values: all positive floating point numbers
 *
 * Note: this value is only relevant if PLANNER_LOOKAHEAD is enabled.
 */
#define JUNCTION_DEVIATION        ((float)START_FEED_RATE)


/* Advance pressure management system settings */
/**
 * Whether to use the advance pressure management system. This system models
 * the extruder as a spring driving an incompressible fluid through the nozzle,
 * which should make the actual flow rate better approach the desired flow rate.
 *
 * Valid values: 0 or 1
 */
#define ADVANCE_ALGORITHM         1

/**
 * The density (in g/mm³) of the filament.
 *
 * - PLA has a density of about 1300 kg/m³, which is 0.0013 g/mm³
 * - ABS has a density of about 1100 kg/m³, which is 0.0011 g/mm³
 *
 * Valid values: positive floating point values
 *
 * Note: this value is only relevant if \a ADVANCE_ALGORITHM is enabled.
 */
#define FILAMENT_DENSITY       0.0013

/**
 * The k-value (in g/min²) of the spring that models the extruder. This is the
 * k from Hooke's law. The higher this value, the less the advance algorithm
 * will advance the extruder.
 *
 * Valid values: positive floating point values
 *
 * Note: this value is only relevant if \a ADVANCE_ALGORITHM is enabled.
 */
#define EXTRUDER_K              1200000

/**
 * The number of output millimeters to reverse the extruder when no filament
 * needs to be extruded, to prevent oozing. Note that even when this parameter
 * is set to 0, the extruder will still appear to reverse when no filament
 * needs to be extruded, just because the extruder needs to undo the advance
 * steps it took when extruding previously. 
 *
 * Valid values: positive floating point values
 *
 * Note: this value is only relevant if \a ADVANCE_ALGORITHM is enabled.
 */
#define ADVANCE_DEPRIME_MM_OUT   5

/**
 * Determines whether to base the advance calculations on the extruder's feed
 * rate or on the 'overall' feed rate (as specified in the g-code commands).
 * Normally, these two should coincide but the host software might want to pull
 * some tricks which makes them differ. To support these tricks, this parameter
 * should be turned ON. However, this seems to give some problems due to
 * round-off errors, so it is recommended to leave this parameter OFF. More 
 * information can be found in \a advance.h.
 *
 * Valid values: 0 or 1
 *
 * Note: this value is only relevant if \a ADVANCE_ALGORITHM is enabled.
 */
#define ADVANCE_BASE_FEEDRATE_E 0



/* More technical settings */
/**
 * The size of the planning queue in number of movements. The planning queue
 * stores movements that have been generated by the planner component (based on
 * incoming g-code commands) and are waiting to be executed by the block
 * handler component.
 *
 * This size determines the number of movements that can be waiting for
 * execution. If this size is set too low, the machine will start pausing
 * between movements at high print speeds. The size of the planning queue also
 * directly determines how many moves the lookahead component will look ahead
 * to optimize the speed. If the size is set too large, the lookahead component
 * will consume too much CPU time (if enabled).
 *
 * Valid values: positive integers from 1 to 128
 */
#define PLANNER_QUEUE_SIZE 32

/**
 * Whether to hold the extruder's stepper motors when it is not stepping. This
 * prevents the motor from moving but consumes power and continues to heat
 * up the motor. It is HIGHLY recommended to enable this for for the e axis,
 * because the motor will start reversing under the pressure of the filament
 * otherwise.
 *
 * Valid values: 0 or 1
 */
#define STEPPER_HOLD_E   1

/**
 * The size of the receive buffer of USART0. This determines the maximum number
 * of characters that can be waiting to be read by the g-code component.
 *
 * Valid values: positive integers from 1 to more than 1024
 */
#define USART0_RECEIVE_BUFFER_SIZE 128

/**
 * The size of the gcode component's command buffer. This determines the
 * maximum number of characters of g-code commands that can be processed by the
 * g-code component.
 *
 * Valid values: positive integers from 1 to more than 1024
 */
#define GCODE_BUFFER_SIZE  64


/**
 * Whether to hold the x axis' stepper motor when it is not stepping. This
 * prevents the motor from moving but consumes power and continues to heat
 * up the motor. It is recommended to enable this for the x axis.
 *
 * Valid values: 0 or 1
 */
#define STEPPER_HOLD_X   1

/**
 * Whether to hold the y axis' stepper motor when it is not stepping. This
 * prevents the motor from moving but consumes power and continues to heat
 * up the motor. It is recommended to enable this for for the y axis.
 *
 * Valid values: 0 or 1
 */
#define STEPPER_HOLD_Y   1

/**
 * Whether to hold the z axis' stepper motors when they are not stepping. This
 * prevents the motors from moving but consumes power and continues to heat
 * up the motor. It is recommended to disable this for the z axis, because
 * it can't freely move up and down even when the motors are disabled.
 *
 * Valid values: 0 or 1
 */
#define STEPPER_HOLD_Z   0


#endif // CONFIG_H
